import { createRouter, createWebHistory } from 'vue-router'
import {unauthorized} from "@/net/index.js";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'login',
            component: () => import('@/views/BaseLoginView.vue'),
            children: [
                {
                    path: '',
                    name: 'login-u',
                    component: () => import('@/views/welcome/LoginView.vue'),
                },
                {
                    path: 'register',
                    name: 'register',
                    component: () => import('@/views/welcome/RegisterView.vue'),
                },
                {
                    path: 'reset',
                    name: 'reset',
                    component: () => import('@/views/welcome/ResetView.vue'),
                }
            ]
        },
        {
            path: '/manager',
            name: 'manager',
            redirect:"/manager/manager_home",
            component:()=>import('@/views/Manager/Manager.vue'),
            children: [
                {
                    path: '/manager/manager_home',
                    name: 'manager_home',
                    meta: { name: "首页统计" },
                    component: () => import("@/views/manager/Home.vue"),
                },
                {
                    path: '/manager/users',
                    name: 'users',
                    meta: { name: "普通用户" },
                    component: () => import("@/views/manager/User.vue"),
                },
                {
                    path: '/manager/notice',
                    name: 'notice',
                    meta: { name: "公告信息" },
                    component: () => import("@/views/manager/Notice.vue"),
                },
                {
                    path: '/manager/manager_team',
                    name: 'manager_team',
                    meta: { name: "团队信息" },
                    component: () => import("@/views/manager/Team.vue"),
                },
                {
                    path: '/manager/teamType',
                    name: 'teamType',
                    meta: { name: "团队服务类型" },
                    component: () => import("@/views/manager/TeamType.vue"),
                },
                {
                    path: '/manager/teamEmp',
                    name: 'teamEmp',
                    meta: { name: "员工管理" },
                    component: () => import("@/views/manager/TeamEmp.vue"),
                },
                {
                    path: '/manager/comment',
                    name: 'comment',
                    meta: { name: "评论信息" },
                    component: () => import("@/views/manager/Comment.vue"),
                },
                {
                    path: '/manager/order',
                    name: 'order',
                    meta: { name: "订单信息" },
                    component: () => import("@/views/manager/Order.vue"),
                },
                {
                    path: '/manager/person',
                    name: 'person',
                    meta: { name: "个人信息" },
                    component:()=>import('@/components/PersonalInfo.vue'),

                },
                {
                    path: '/manager/password',
                    name: 'password',
                    meta: { name: "修改密码" },
                    component:()=>import('@/components/UpdatePassword.vue'),

                },
                {
                    path: '/manager/priceRule',
                    name: 'priceRule',
                    meta: { name: "计费规则" },
                    component:()=>import('@/views/manager/PriceRule.vue'),

                },
            ]
        },

        {
            path: '/user',
            name: 'user',
            redirect:"/user/user_home",
            component:()=>import('@/views/User/User.vue'),
            children: [
                {
                    path: '/user/user_home',
                    name: 'user_home',
                    meta: { name: "首页" },
                    component: () => import("@/views/User/Home.vue"),
                },
                {
                    path: '/user/user_notice',
                    name: 'user_notice',
                    meta: { name: "网站公告" },
                    component: () => import("@/views/User/Notice.vue"),
                },
                {
                    path: '/user/user_notice/:id',
                    name: 'notice_detail',
                    component: () => import('@/views/User/NoticeDetail.vue')
                },
                {
                    path: '/user/user_charge',
                    name: 'user_charge',
                    component: () => import('@/views/User/Charge.vue')
                },
                {
                    path: '/user/user_team',
                    name: 'user_team',
                    component: () => import('@/views/User/Team.vue')
                },
                {
                    path: '/user/user_team_detail/:id',
                    name: 'user_team_detail',
                    component: () => import('@/views/User/TeamDetail.vue')
                },
                {
                    path: '/user/user_appointment',
                    name: 'user_appointment',
                    component: () => import('@/views/User/Appointment.vue')
                },
                {
                    path: '/user/pay/success',  // 改成和用户路由一致的路径
                    name: 'PaySuccess',
                    component: () => import('@/views/User/PaySuccess.vue')
                },
                {
                    path: '/user/person',
                    name: 'user_person',
                    meta: { name: "个人信息" },
                    component:()=>import('@/components/PersonalInfo.vue'),

                },
                {
                    path: '/user/password',
                    name: 'user_password',
                    meta: { name: "修改密码" },
                    component:()=>import('@/components/UpdatePassword.vue'),

                },
                {
                    path: '/user/order',
                    name: 'user_order',
                    meta: { name: "我的订单" },
                    component:()=>import('@/views/User/MyOrder.vue'),

                },

            ]
        },

        {
            path: '/team',
            name: 'team',
            redirect:"/team/team_home",
            component:()=>import('@/views/Team/Team.vue'),
            children: [
                {
                    path: '/team/team_home',
                    name: 'team_home',
                    meta: { name: "首页" },
                    component: () => import("@/views/Team/Home.vue"),
                },
                {
                    path: '/team/my_team',
                    name: 'my_team',
                    meta: { name: "我的团队" },
                    component: () => import("@/views/Team/MyTeam.vue"),
                },
                {
                    path: '/team/team_order',
                    name: 'team_order',
                    meta: { name: "订单信息" },
                    component: () => import("@/views/Team/Order.vue"),
                },
                {
                    path: '/team/team_comment',
                    name: 'team_comment',
                    meta: { name: "订单信息" },
                    component: () => import("@/views/Team/Comment.vue"),
                },
            ]
        }




    ]
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
    // 获取用户信息
    const userStr = localStorage.getItem('user')
    const user = userStr ? JSON.parse(userStr) : null

    // 如果要访问管理员页面
    if (to.path.startsWith('/manager')) {
        if (user && user.role === 1) {
            // 是管理员，允许访问
            next()
        } else {
            // 不是管理员，重定向到登录页
            next('/login')
        }
    }
    // 如果要访问普通用户页面
    else if (to.path.startsWith('/user')) {
        if (user && user.role === 3) {
            // 是普通用户，允许访问
            next()
        } else {
            // 不是普通用户，重定向到登录页
            next('/login')
        }
    }
    // 其他页面直接放行
    else {
        next()
    }
})

export default router